package cn.xspace.wnx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xspace.android.xafcommon.base.BaseActivity;
import com.xspace.android.xafcommon.been.BaseReturnBean;
import com.xspace.android.xafcommon.util.Constants;
import com.xspace.android.xafcommon.util.LogUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.xspace.app.view.TitleBar;
import cn.xspace.library.util.MyInfoUtil;
import cn.xspace.library.util.OkHttpManager;
import cn.xspace.wnx.R;
import cn.xspace.wnx.adapter.SearchResultAdaper;
import cn.xspace.library.bean.OrgDeptUserBean;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class SearchForAddFriendActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = SearchForAddFriendActivity.class.getSimpleName();

    private TitleBar mTitleBar;
    private EditText mEditSearch;
    private RelativeLayout mRelativeSearch;
    private ImageView mIvSearchEmpty;
    private RelativeLayout mSearchEmpty;
    private ListView mListView;
    private SearchResultAdaper resultAdapter;
    private ArrayList<OrgDeptUserBean> mDataList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void getIntentData() {
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_search_for_add_friend);
    }

    @Override
    protected void initFindViewById() {
        mTitleBar = (TitleBar) findViewById(R.id.add_friend_titleBar);
        mTitleBar.setRightLayoutVisibility(View.GONE);
        mEditSearch = (EditText) findViewById(R.id.edit_add_friend_search);
        mRelativeSearch = (RelativeLayout) findViewById(R.id.rl_add_friend__search);
        mRelativeSearch.setOnClickListener(this);
        mSearchEmpty = (RelativeLayout) findViewById(R.id.search_empty);
        mListView = (ListView) findViewById(R.id.lv_search_result);
    }

    @Override
    protected void initData() {
        mDataList = new ArrayList<>();
//        resultAdapter = new SearchResultAdaper(mContext, mDataList);
//        mListView.setAdapter(resultAdapter);
    }

    @Override
    protected void initEvent() {
        mEditSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                //当actionId == XX_SEND 或者 XX_DONE时都触发
                //或者event.getKeyCode == ENTER 且 event.getAction == ACTION_DOWN时也触发
                //注意，这是一定要判断event != null。因为在某些输入法上会返回null。
                if (actionId == EditorInfo.IME_ACTION_SEARCH
                        || actionId == EditorInfo.IME_ACTION_SEND
                        || actionId == EditorInfo.IME_ACTION_DONE
                        /*|| (event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN ==
                        event.getAction())*/) {
                    //处理事件
                    submit();
                }
                return false;
            }
        });
        mTitleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
//        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                OrgDeptUserBean orgDeptUserBean = mDataList.get(position);
//                Intent mIntent = new Intent(mContext, PersonCardActivity.class);
//                mIntent.putExtra(PersonCardActivity.FRIEND_ID, orgDeptUserBean.user_id);
//                startActivity(mIntent);
//            }
//        });
    }

    private void requestSearchFriend(String search) {
        showWaitDialog();
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("user_id", MyInfoUtil.getMyUserid(mContext));
        requestMap.put("search_mobile", search);
        registerSubscription(OkHttpManager.getInstance().commonRequest(Constants.SEARCH_ALL_USER, requestMap)
                .subscribeOn(Schedulers.io())
                .map(new Func1<BaseReturnBean, List<OrgDeptUserBean>>() {
                    @Override
                    public List<OrgDeptUserBean> call(BaseReturnBean baseReturnBean) {
                        List<OrgDeptUserBean> dataList;
                        Gson mGson = new Gson();
                        dataList = mGson.fromJson(baseReturnBean.data, new
                                TypeToken<List<OrgDeptUserBean>>() {
                                }.getType());
                        return dataList;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<OrgDeptUserBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        hideWaitDialog();
                        e.printStackTrace();
                        mSearchEmpty.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onNext(List<OrgDeptUserBean> dataList) {
                        hideWaitDialog();
                        mSearchEmpty.setVisibility(View.GONE);
                        mDataList.clear();
                        LogUtil.d(TAG, "dataListSize>>>>>" + dataList.size());
                        if (dataList.size() > 0) {
//                            mDataList.addAll(dataList);
                            OrgDeptUserBean orgDeptUserBean = dataList.get(0);
                            Intent mIntent = new Intent(mContext, PersonCardActivity.class);
                            mIntent.putExtra(PersonCardActivity.FRIEND_ID, orgDeptUserBean.user_id);
                            startActivity(mIntent);
                        } else {
                            mSearchEmpty.setVisibility(View.VISIBLE);
                        }
//                        resultAdapter.notifyDataSetChanged();
                    }
                }));
    }

    private void submit() {
        String search = mEditSearch.getText().toString().trim();
        if (TextUtils.isEmpty(search)) {
            Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        requestSearchFriend(search);
    }

    @Override
    public void onClick(View v) {

    }
}
